﻿namespace Digitalmes.Application.Common.Strategies;

/// <summary>
/// 策略注册器工厂。
/// </summary>
internal sealed class StrategyRegisterFactory(IServiceProvider serviceProvider) : IStrategyRegisterFactory
{
    private static readonly HashSet<string> Strategies = [];

    /// <summary>
    /// 注册策略。
    /// </summary>
    public static void Register(string policyName)
    {
        if (!Strategies.Add(policyName))
        {
            throw new InvalidOperationException($"已注册名称为 {policyName} 的业务策略，不能再重复注册。");
        }
    }

    public IStrategy? GetStrategy(string name)
    {
        return serviceProvider.GetKeyedService<IStrategy>(name);
    }
}
